package com.web.music.portal.mvc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;

import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.web.music.interfaces.ArtistService;
import com.web.music.interfaces.PersonService;
import com.web.music.interfaces.GenreService;
import com.web.music.interfaces.SecurityController;
import com.web.music.model.Genre;
import com.web.music.model.Person;
import com.web.music.model.Singer;

public class SingerFormController extends SimpleFormController {

	private ArtistService singerService;
	private PersonService personService;
	private GenreService  genreService;
	private SecurityController securityController;
	//static Logger logger = Logger.getLogger("Logger");	
	
	public SingerFormController() {
		setCommandClass(Singer.class);
		setSessionForm(true);
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request) {		
		Singer singer = new Singer();
		Person dummyPerson = new Person();
		singer.setPerson(dummyPerson);
		singer.setCreatedBy(securityController.getSessionUser(request));
		// execute the following code only when going to display a form
		if (!isFormSubmission(request)){	
			String idString = request.getParameter("id");
			if (idString != null && !idString.equals("")) {
				long id = Long.parseLong(request.getParameter("id"));
				singer = (Singer)singerService.getById(id);
			}		
		}	
		securityController.updateSecurityStatus(request, singer);
		return singer;		
	}

	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Person.class, new PersonPropertyEditor(personService));
		binder.registerCustomEditor(Genre.class, new GenrePropertyEditor(genreService));
		super.initBinder(request, binder);
	}	
	
	
	/*@Override
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object object, BindException errors) 
	throws Exception
     {
		Singer  singer = (Singer)object;	
		Person  person;
		String idPerson;
		idPerson = request.getParameter("person");
		if (idPerson != null && !idPerson.equals("0")) {
			long id = Long.parseLong(idPerson);
			person = personService.getById(id);
			singer.setPerson(person);
		}		
		singerService.save(singer);
		
		return new ModelAndView(getSuccessView());
	}*/
	
	
	@Override
    protected Map<String,Object> referenceData(HttpServletRequest request) throws Exception 
    {

        Map<String,Object> refData = new HashMap<String,Object>();
        List<Person> allpeople = personService.findAll();
        List<Genre> allgenres = genreService.findAll();
        refData.put("people",allpeople);
        refData.put("genres",allgenres);
        return refData;
    }
	
	@Override
	public void doSubmitAction(Object object){
		Singer  singer = (Singer)object;	
		singerService.save(singer);	
	}	
	
	public ArtistService getSingerService() {
		return singerService;
	}

	public void setSingerService(ArtistService singerService) {
		this.singerService = singerService;
	}
	public PersonService getPersonService(){
		return personService;
	}
	public void setPersonService(PersonService personService){
		this.personService = personService;
	}
	public GenreService getGenreService(){
		return this.genreService;
	}
	public void setGenreService(GenreService genreService){
		this.genreService = genreService;
	}
	public SecurityController getSecurityController() {
		return securityController;
	}

	public void setSecurityController(
			SecurityController securityController) {
		this.securityController = securityController;
	}		
}
